//
//  CouponView.m
//  U_APP
//
//  Created by angle yan on 2017/11/21.
//  Copyright © 2017年 Ezra. All rights reserved.
//

#import "CouponView.h"

@interface CouponView ()
{
    UIBezierPath *lPath;
    
}

@end
@implementation CouponView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.layer.shadowOffset = CGSizeMake(0, 0);
        self.layer.shadowOpacity = 0.35f;
        self.layer.shadowRadius = 4;
        self.layer.shadowColor = [UIColor clearColor].CGColor;
        self.radius = 4;
        self.leftW = 100;
        self.rightW = 68;
        self.semicircle = 16;
        [self setNeedsDisplay];
    }
    return self;
}

- (void)setShadowOpacity:(float)shadowOpacity
{
    _shadowOpacity = shadowOpacity;
    self.layer.shadowOpacity = shadowOpacity;
}
- (void)setShadowRadius:(CGFloat)shadowRadius
{
    _shadowRadius = shadowRadius;
    self.layer.shadowRadius = shadowRadius;
}
- (void)setShadowColor:(UIColor *)shadowColor
{
    _shadowColor = shadowColor;
    self.layer.shadowColor = shadowColor.CGColor;
}

+ (CouponView *)getCouponView
{
    CouponView *coupon = [[CouponView alloc] initWithFrame:CGRectMake(0, 0, 340, 100)];
    return coupon;
}



- (void)drawRect:(CGRect)rect {
 
    CGFloat c_heigth = CGRectGetHeight(rect);
    CGFloat c_width = CGRectGetWidth(rect);
    
    lPath = [UIBezierPath bezierPath];
    [lPath moveToPoint:CGPointMake(self.leftW-self.semicircle, 0)];
    [lPath addArcWithCenter:CGPointMake(self.leftW, 0) radius:self.semicircle startAngle:M_PI endAngle:M_PI_2 clockwise:NO];
    [lPath addArcWithCenter:CGPointMake(self.leftW, c_heigth) radius:self.semicircle startAngle:-M_PI_2 endAngle:M_PI clockwise:NO];

    [lPath addArcWithCenter:CGPointMake(self.radius, c_heigth-self.radius) radius:self.radius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
    [lPath addArcWithCenter:CGPointMake(self.radius, self.radius) radius:self.radius startAngle:M_PI endAngle:-M_PI_2 clockwise:YES];
    [[UIColor whiteColor] set];
    [lPath fill];
    

    UIBezierPath *rPath = [UIBezierPath bezierPath];
    [rPath moveToPoint:CGPointMake(self.leftW, self.semicircle)];
    [rPath addArcWithCenter:CGPointMake(self.leftW, 0) radius:self.semicircle startAngle:M_PI_2 endAngle:-M_PI clockwise:NO];
    [rPath addArcWithCenter:CGPointMake(c_width-self.radius, self.radius) radius:self.radius startAngle:-M_PI_2 endAngle:0 clockwise:YES];
    [rPath addArcWithCenter:CGPointMake(c_width-self.radius, c_heigth-self.radius) radius:self.radius startAngle:0 endAngle:M_PI_2 clockwise:YES];
    [rPath addArcWithCenter:CGPointMake(self.leftW,c_heigth) radius:self.semicircle startAngle:0 endAngle:-M_PI_2 clockwise:NO];
    UIColor *rcolor = [UIColor whiteColor];
    [rcolor set];
    [rPath fill];
    
    UIBezierPath *lineP = [UIBezierPath bezierPath];
    [lineP moveToPoint:CGPointMake(c_width-self.rightW, 0)];
    [lineP addLineToPoint:CGPointMake(c_width-self.rightW, c_heigth)];
    UIColor *lineColor = self.sepLineColor;
    [lineColor set];
    CGFloat fl[] = {5,5};
    [lineP setLineDash:fl count:2 phase:5];
    [lineP stroke];

    [self change];
}


- (void)change
{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    gradientLayer.colors = self.colors;
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = lPath.CGPath;
    [gradientLayer setMask:maskLayer];
    [self.layer addSublayer:gradientLayer];

}






@end
